package com.tender;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class EasyTestAdvance4InclusivegatewayAPI {

    /**
     * 流程部署
     */
    @Test
    public void test01() {
        // 1.获取ProcessEngine对象
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RepositoryService进行部署操作
        RepositoryService service = engine.getRepositoryService();
        // 3.使用RepositoryService进行部署操作
        Deployment deploy = service.createDeployment()
                .addClasspathResource("bpmn/tender_evection_inclusivegateway.bpmn") // 添加bpmn资 源
                .addClasspathResource("bpmn/tender_evection_inclusivegateway.png") // 添加png资源
                .name("出差申请流程-inclusivegateway")
                .deploy();// 部署流程
        // 4.输出流程部署的信息
        System.out.println("流程部署的id:" + deploy.getId());
        System.out.println("流程部署的名称：" + deploy.getName());

        /**
         * 流程部署的id:87501
         * 流程部署的名称：出差申请流程-inclusivegateway
         */
    }

    /**
     * 启动流程实例
     */
    @Test
    public void test02() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = engine.getRuntimeService();
        // 流程定义key
        // 查看表：act_re_procdef 的 KEY_ 字段获得
        String key = "tender_evection_InclusiveGateway";

        Map<String, Object> map = new HashMap<>();
        Evection evection = new Evection();
        evection.setNum(4d);
        map.put("evection", evection);

        // 启动实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, map);
        // 输出信息
        System.out.println("获取流程实例名称：" + processInstance.getName());
        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());

        /**
         * 获取流程实例名称：null
         * 流程定义ID：tender_evection_InclusiveGateway:1:87504
         */
    }

    /**
     * 负责人：creater_tender
     * 完成个人任务
     */
    @Test
    public void test03() {
        String key = "tender_evection_InclusiveGateway";
        String assignee = "creator_tender";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                //.taskCandidateUser(candidateUser)
                //.taskCandidateOrAssigned(candidateUser)
                .taskAssignee(assignee)
                .singleResult();

        taskService.complete(task.getId());
        System.out.println("完成任务：" + task.getName());
    }

    /**
     * 负责人：hr_manager_tender
     * 完成个人任务
     */
    @Test
    public void test05() {
        String key = "tender_evection_InclusiveGateway";
        String assignee = "hr_manager_tender";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                //.taskCandidateUser(candidateUser)
                //.taskCandidateOrAssigned(candidateUser)
                .taskAssignee(assignee)
                .singleResult();

        taskService.complete(task.getId());
        System.out.println("完成任务：" + task.getName());
    }

    /**
     * 负责人：pm_manager_tender
     * 完成个人任务
     */
    @Test
    public void test06() {
        String key = "tender_evection_InclusiveGateway";
        String assignee = "pm_manager_tender";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                //.taskCandidateUser(candidateUser)
                //.taskCandidateOrAssigned(candidateUser)
                .taskAssignee(assignee)
                .singleResult();

        taskService.complete(task.getId());
        System.out.println("完成任务：" + task.getName());
    }

    /**
     * 负责人：pm_manager_tender
     * 完成个人任务
     */
    @Test
    public void test07() {
        String key = "tender_evection_InclusiveGateway";
        String assignee = "general_manager_tender";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                //.taskCandidateUser(candidateUser)
                //.taskCandidateOrAssigned(candidateUser)
                .taskAssignee(assignee)
                .singleResult();

        taskService.complete(task.getId());
        System.out.println("完成任务：" + task.getName());
    }

}
